<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="地区" prop="AreaAddress">
        <CityPicker ref="cityPicker" size="small" clearable placeholder="请选择" v-model="queryParams.AreaAddress" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['gongyuan:area:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="省" prop="province" :show-overflow-tooltip="true" />
      <el-table-column label="市" prop="city" :show-overflow-tooltip="true" />
      <el-table-column label="区" prop="area" :show-overflow-tooltip="true" />
      <el-table-column label="是否支付定金" :show-overflow-tooltip="true" :formatter="paymentTypeFormatter" />
      <el-table-column label="定金金额" prop="amount" :show-overflow-tooltip="true" />
      <el-table-column label="额外费用(满)" prop="extraCosts" :show-overflow-tooltip="true" />
      <el-table-column  width="280" label="服务人员额外费用提成比例(%)" prop="serviceRate" :show-overflow-tooltip="true" />
      <el-table-column label="方案费用付款方式" prop="area" :show-overflow-tooltip="true" :formatter="payTypeFormatter" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" class="gongyuan-update" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['gongyuan:area:edit']">修改</el-button>
          <el-button size="mini" class="gongyuan-delete" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['gongyuan:area:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import CityPicker from '@/components/cityPicker'
import { getAreaList, delArea } from '@/api/area/index'
export default {
  name: 'Arealist',
  components: {
    CityPicker
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      list: [],
      total: 0,
      // 查询参数
      queryParams: {
        AreaAddress: [],
        pageNum: 1,
        pageSize: 10
      },
      dealerTypeOptions: [],
      dealerStatusOptions: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async initDictsOptions(dicts, listProps) {
      try {
        const res = await this.getDicts(dicts)
        if (res.code === 200) {
          this[listProps] = res.data.map((per) => ({
            label: per.dictLabel,
            value: per.dictValue
          }))
        }
      } catch (error) {
        console.log(error)
      }
    },
    paymentTypeFormatter(row){
      return row.paymentType === 'true' ? '是' : '否'
    },
    payTypeFormatter(row){
      return row.payType === '1' ? '一次性' : '分期'
    },
    async getList() {
      this.loading = true
      let cityObj
      if (!this || !this.$refs.cityPicker) {
        cityObj = {
          province: '',
          city: '',
          area: ''
        }
      } else {
        cityObj = this.$refs.cityPicker.getValueTextObj(this.queryParams.AreaAddress)
      }

      const { AreaAddress, ...other } = this.queryParams

      const res = await getAreaList({
        ...other,
        province: cityObj.province,
        city: cityObj.city,
        area: cityObj.area
      })
      this.list = res.rows
      this.total = res.total
      this.loading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    handleAdd() {
      // 此处后台管理添加的路由, path对应组件name须一致,不可再修改
      this.$router.push({ name: 'Areaedit' })
    },

    handleDetail(row) {
      this.$router.push({ name: 'Areadetail', query: { id: row.id } })
    },
    handleEdit(row) {
      this.$router.push({ name: 'Areaedit', query: { id: row.id } })
    },
    handleDelete(row) {
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delArea(row.id)
        })
        .then(() => {
          this.queryParams.pageNum = 1
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    }
  }
}
</script>

